package cn.com.lyb.job.service.impl;

import cn.com.lyb.job.dao.JobInstanceDao;
import cn.com.lyb.job.entity.JobInstanceDO;
import cn.com.lyb.job.entity.vo.JobInstanceVO;
import cn.com.lyb.job.service.IJobInstanceService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 任务实例表 服务实现类
 * </p>
 *
 * @author lvyuanbo
 * @since 2024-07-11
 */
@Service
public class JobInstanceServiceImpl extends ServiceImpl<JobInstanceDao, JobInstanceDO> implements IJobInstanceService {

    @Autowired
    private JobInstanceDao jobInstanceDao;
    /**
     * selectPage
     *
     * @param status
     * @param start
     * @param length
     * @return
     */
    @Override
    public List<JobInstanceVO> selectPage(String status, Integer start, Integer length, Long jobId, Long workflowInstanceId) {
        PageHelper.startPage(start, length);
        return jobInstanceDao.selectPageByStatus(status, jobId, workflowInstanceId);
    }

    /**
     * 检测当前任务是否可以运行
     *
     * @param id
     * @return
     */
    @Override
    public JobInstanceDO checkRunByJobId(Long id) {
        LambdaQueryWrapper<JobInstanceDO> wrapper = new LambdaQueryWrapper<JobInstanceDO>();
        wrapper.eq(JobInstanceDO::getJobId, id);
        wrapper.orderByDesc(JobInstanceDO::getId);
        return jobInstanceDao.selectOne(wrapper);
    }
}
